<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/23 0023
 * Time: 下午 3:20
 * Author: NanTao <260053530@qq.com>
 */
namespace app\index\controller;
use think\Db;
class Order extends Common
{
    public function index()
    {
        $orderAll = $this->getOrderData();
        $this->assign('orderAll',$orderAll);
        $order1 = $this->getOrderData(0);
        $this->assign('order1',$order1);
        $order2 = $this->getOrderData(1);
        $this->assign('order2',$order2);
        $order3 = $this->getOrderData(2);
        $this->assign('order3',$order3);
        $order4 = $this->getOrderData(5);
        $this->assign('order4',$order4);
        return $this->fetch();
    }
    /**
     * 获取订单信息
     * @return bool
     */
    public function getOrderData($status)
    {
        $userId = session('userId');
        $where = ['user_id'=>$userId];
        isset($status) ? $where['status'] = $status : '' ;
        $order = Db::name('goods_order')->where($where)->order('add_time desc')->select();
        foreach ($order as $k=>$v)
        {
            $order[$k]['goods'] = Db::name('goods_order_detail')->where('order_id',$v['id'])->select();
        }
        return $order;
    }
    public function show()
    {
        $orderId = input('param.orderId');
        $order = Db::name('goods_order')->where('id',$orderId)->find();
        $order['goods'] = Db::name('goods_order_detail')->where('order_id',$order['id'])->select();
        $this->assign('order',$order);
        return $this->fetch();
    }
    /*
     * 订单页面
     * */
    public function order()
    {
        $userId = session('userId');
        $cartIds = input('param.cartIds');
        $this->assign('cartId',$cartIds);
        $cartIdArr = array_filter(explode(',',$cartIds));
        $data = [];
        $sum = '';
        foreach ($cartIdArr as $k=>$v)
        {
            $cart = Db::name('cart')->where('id',$v)->find();
            $data['goods'][] = $cart;
            $sum += $cart['goods_sell_price'] * $cart['goods_count'];
            $data['sum'] = $sum;
        }
        $this->assign('list',$data);
        $address = Db::name('user_address')->where(['user_id'=>$userId,'is_default'=>1])->find();
        if ($address)
        {
            $cost = get_express_cost($address['province_code']);
            $this->assign('cost',$cost);
            $address['province'] = province($address['province_code']);
            $address['city'] = city($address['city_code']);
            $address['area'] = area($address['area_code']);
            $this->assign('address',$address);
        }

        $coupon = get_user_coupon($userId,0,$sum);
        $this->assign('coupon',$coupon);
        return $this->fetch();
    }
    /*
     * 添加订单
     * */
    public function add()
    {
        $userId = session('userId');
        $addressId = input('param.addressId');
        $sum = input('param.sum');
        $cost = input('param.cost');
        $cartId = input('param.cartId');
        $cartId = array_filter(explode(',',$cartId));
//        $payType = input('param.payType');
        $addressData  = Db::name('user_address')->find($addressId);
        $address = province($addressData['province_code']).city($addressData['city_code']).area($addressData['area_code']).$addressData['info'];
        $name = $addressData['name'];
        $mobile = $addressData['mobile'];
        $couponMoney = input('param.couponMoney');
        $couponId = input('param.couponId');
        if ($couponId)
        {
            Db::name('user_coupon')->delete($couponId);
        }
        $orderNo='GM'.date('ymdHis',time()).mt_rand(1000,9999);
        $order = [
//            'pay_type'=>$payType,
            'order_no'=>$orderNo,
            'sum' => $sum,
            'cost' => $cost,
            'name'=>$name,
            'mobile'=>$mobile,
            'address'=>$address,
            'user_id'=>$userId,
            'coupon_money'=>$couponMoney,
            'pay_money'=>$sum+$cost-$couponMoney
        ];
        if (Db::name('goods_order')->insert($order))
        {
            $orderId = Db::name('goods_order')->getLastInsID();
            $openId = get_user_info_data($userId,'wx_openid');
            foreach ($cartId as $k=>$v)
            {
                $cart = Db::name('cart')->where('id',$v)->find();
                $data[] = [
                    'order_id' => $orderId,
                    'goods_title'=>$cart['goods_title'],
                    'goods_id'=>$cart['goods_id'],
                    'goods_img_url'=>$cart['goods_img_url'],
                    'goods_sell_price'=>$cart['goods_sell_price'],
                    'goods_count'=>$cart['goods_count'],
                    'user_id' => $userId,
                    'wx_openid'=>$openId,
                    'order_no' => $orderNo
                ];
            }
            Db::name('cart')->delete($cartId);
            $result = Db::name('goods_order_detail')->insertAll($data);
            json_msg_data(0,'订单已提交',$orderNo);
        } else {
            json_msg(1,'订单提交失败,请稍候再试');
        }
    }
    /*
     * 确认收货
     * */
    public function confirm()
    {
        $orderId = input('param.orderId');
        $result = Db::name('goods_order')->where('id',$orderId)->update(['status'=>5,'confirm_time'=>date('Y-m-d H:i:s')]);
        if ($result || $result>0)
        {
            json_msg(0,'已经确认收货');
        } else {
            json_msg(1,'系统错误,操作失败,请稍候再试');
        }
    }
    /*
     * 删除
     * */
    public function del()
    {
        $orderId = input('param.orderId');
        $result = Db::name('goods_order')->where('id',$orderId)->delete();
        $row = Db::name('goods_order_detail')->where('order_id',$orderId)->delete();
        if ($result || $row)
        {
            json_msg(0,'已删除');
        } else {
            json_msg(1,'系统错误,操作失败,请稍候再试');
        }
    }
}